<%-- 
    Document   : search
    Created on : Oct 8, 2012, 9:58:33 PM
    Author     : Nhan
--%>

<%@page import="com.Cart"%>
<%@page import="com.Book"%>
<%@page import="com.Checker"%>
<%@page import="java.util.ArrayList" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Search Book Page</title>
    </head>
    <body>
        <h3>Your are current in: <a href="index.jsp">Home</a> &gt; <a href="search.jsp">Search</a></h3>
        <form action="centralUnit" method="GET">
            <fieldset>
                <legend>Search Panel</legend>
                <table>
                    <tr>
                        <td><input type="text" name="keyword" value="" size="" /></td>
                        <td><select name="filter">
                                <option>Title</option>
                                <option>Author</option>
                                <option>Type</option>
                            </select>
                        </td>
                        <td><input type="submit" name="action" value="Search" size="" /></td>
                    </tr>
                </table>
            </fieldset>
        </form>

        <%

                    String keyword = request.getParameter("keyword");
                    String filter = request.getParameter("filter");
                    String action = request.getParameter("action");

                    if (action != null && action.equals("Search")) {%>
        <h3>Search result for: <b><i><%= keyword%></i></b>  in  <b><i><%= filter%></i></b> </h3>
        
        <table border="1" cellspacing="0">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Book ID</th>
                    <th>Title</th>
                    <th>Image</th>
                    <th>Author</th>
                    <th>Type</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>
                <%
                    ArrayList<Book> searhResult = Checker.getSearchResult(keyword, filter);
                    int index = 0;
                    if (searhResult.size() != 0) {
                        for (Book book : searhResult) {
                            index++;
                            int id = book.getId();
                            String title = book.getTitle();
                            String author = book.getAuthor();
                            String image = book.getImage();
                            String type = book.getType();
                            float price = book.getPrice();
                            String url = "centralUnit?action=viewBook&bookID=" + id;
                            String encodedUrl = response.encodeRedirectURL(url);
                %>

                <tr>
                    <td><%= index%></td>
                    <td><%= id %></td>
                    <td><a href="<%= encodedUrl %>"><%= title %></a></td>
                    <td><img src="images/<%= image %>" height="30px" width="30px" alt="Images Description for <%= title %>" /></td>
                    <td><%= author %></td>
                    <td><%= type %></td>
                    <td><%=price %></td>
                    
                    <td>
                        <form action="centralUnit" method="GET">
                           <input type="hidden" name="bookID" value="<%= id %>" />
                           <input type="submit" name="action" value="Add To Cart" size="" /> 
                        </form>
                    </td>
                </tr>
                <% }
                           } else {%>

                <tr><td colspan="6"><h3>0 result, Please try another keyword!</h3></tr>

                <% }

                %>
            </tbody>
        </table>
        <% }
            session = request.getSession();
            ArrayList<Cart> selectedItems = ( ArrayList<Cart>)session.getAttribute("selectedItems");
            if(selectedItems != null && selectedItems.size() != 0) { %>
            <p><a href="centralUnit?action=viewCart">View Shopping Cart!</a></p>
            <% }
        %>
    </body>
</html>
